package com.cskaoyan.shopping.controller.yyr;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.dto.yyr.AllProductCateRequest;
import com.cskaoyan.shopping.dto.yyr.AllProductCateResponse;
import com.cskaoyan.shopping.dto.yyr.HomePageResponse;
import com.cskaoyan.shopping.dto.yyr.NavListResponse;
import com.cskaoyan.shopping.service.yyr.IContentService;
import com.cskaoyan.shopping.service.yyr.IHomeService;
import com.cskaoyan.shopping.service.yyr.IProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Description：
 * @Author: 杨涌仁
 * @Date: 2022/08/25/0025 9:07
 */
@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Autowired
    IHomeService iHomeService;

    @Autowired
    IContentService iContentService;

    @Autowired
    IProductCateService iProductCateService;

    /*
     * @Function:
     * 商品主页的显示
     * @author yyr
     */
    @GetMapping("/homepage")
    public ResponseData homepage() {
        // 调用service方法
        HomePageResponse response = iHomeService.homepage();
        // 判断service方法的返回值是否成功，成功则返回数据
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 不成功则返回失败信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /*
     * @Function:
     * 商品导航栏显示
     * @author yyr
     */
    @GetMapping("/navigation")
    public ResponseData navigation() {
        // 调用service方法
        NavListResponse response = iContentService.queryNavList();
        // 判断service方法的返回值是否成功，成功则返回数据
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentDtos());
        }
        // 不成功则返回失败信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /*
     * @Function:
     * 列举商品的所有种类
     * @author yyr
     */
    @GetMapping("/categories")
    // 非必须接收  没有值时默认为1
    public ResponseData categories(@RequestParam(required = false, defaultValue = "1") String sort) {
        AllProductCateRequest request = new AllProductCateRequest();
        if ("1".equals(sort)) {
            // 默认升序
            request.setSort("asc");
        } else {
            request.setSort("desc");
        }
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        // 判断service方法的返回值是否成功，成功则返回数据
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        // 不成功则返回失败信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
